package com.squareshoot.picCheckin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ProfileDb {

	public static final String KEY_JSONPROFILE = "jsonprofile";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_PWD = "pwd";
	public static final String KEY_ROWID = "_id";
	public static final String KEY_COOKIE = "cookie";

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE =
		"create table profile (_id integer primary key autoincrement, "
		+ "jsonprofile text not null, email text not null, pwd text not null,cookie text);";

	private static final String DATABASE_NAME = "picCheckinDb";
	private static final String DATABASE_TABLE = "profile";
	private static final int DATABASE_VERSION = 2;

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			if(Common.HIGHDEBUG)
				Log.w(Common.TAG, "Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS profile");
			onCreate(db);
		}
	}


	public ProfileDb(Context ctx) {
		this.mCtx = ctx;
	}


	public ProfileDb open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public void dropProfileTable(){
		mDb.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
		mDb.execSQL(DATABASE_CREATE);
	}


	public long createProfile(String jsonprofile, String email, String password, String cookie) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_JSONPROFILE, jsonprofile);
		initialValues.put(KEY_EMAIL, email);
		initialValues.put(KEY_PWD, password);
		initialValues.put(KEY_COOKIE, cookie);
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}


	public boolean deleteProfile(long rowId) {
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean deleteProfile(String email) {
		return mDb.delete(DATABASE_TABLE, KEY_EMAIL+ "=" + email, null) > 0;
	}

	public Cursor fetchAllProfiles() {
		return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_JSONPROFILE, KEY_EMAIL,
				KEY_PWD, KEY_COOKIE}, null, null, null, null, null);
	}

	public Cursor fetchProfile(long rowId) throws SQLException {
		Cursor mCursor =
			mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
					KEY_JSONPROFILE, KEY_EMAIL, KEY_PWD, KEY_COOKIE}, KEY_ROWID + "=" + rowId, null,
					null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor recupCookie(long rowId) throws SQLException {
		Cursor mCursor =
			mDb.query(true, DATABASE_TABLE, new String[] {KEY_COOKIE}, KEY_ROWID + "=" + rowId, null,
					null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor recupCookie(String email) throws SQLException {
		Cursor mCursor =
			mDb.query(true, DATABASE_TABLE, new String[] {KEY_COOKIE}, KEY_EMAIL + "=\"" + email+"\"", null,
					null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor recupJsonProfile(String email) throws SQLException {
		Cursor mCursor =
			mDb.query(true, DATABASE_TABLE, new String[] {KEY_JSONPROFILE}, KEY_ROWID + "=" + email, null,
					null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updateProfile(long rowId, String email, String password, String cookie) {
		ContentValues args = new ContentValues();
		args.put(KEY_EMAIL, email);
		args.put(KEY_PWD, password);
		args.put(KEY_COOKIE, cookie);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean updateCookie(long rowId, String cookie) {
		ContentValues args = new ContentValues();
		args.put(KEY_COOKIE, cookie);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}


	public boolean updateEmail(long rowId, String email) {
		ContentValues args = new ContentValues();
		args.put(KEY_EMAIL, email);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean updatePassword(long rowId, String password) {
		ContentValues args = new ContentValues();
		args.put(KEY_PWD, password);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean updateUserProfile(long rowId, String userProfile) {
		// TODO Auto-generated method stub
		ContentValues args = new ContentValues();
		args.put(KEY_JSONPROFILE, userProfile);
		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}